using System;
using System.Data;
using System.Text.RegularExpressions;
using System.Collections;
using System.ComponentModel;
using System.Web;

namespace GS.Utilies
{
	/// <summary>
	/// Author: Le Khac Tri - MCSD.NET
	/// Started Date: 2004 - 06 - 17
	/// Modified Date: 2004 - 07 -13
	/// Goal: Define COMMON funtions
	/// </summary>
	public class ValidFormat
	{
        static string PasswordPattern = @"^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$";
        static string MemberNoPattern = @"^d{13}$";
        static string SeriPattern = @"^d{8,9}$";
        static string OrderNoPattern = @"^d{5}$";
        static string IDNoPattern = @"^d{9}$";
		public ValidFormat()
		{
			//
			// TODO: Add constructor logic here
			//
		}
        public static bool CheckIDNo(string szInput)
        {
            return Regex.IsMatch(szInput, IDNoPattern);
        }

        public static bool CheckOrderNo(string szInput)
        {
            return Regex.IsMatch(szInput, OrderNoPattern);
        }
        public static bool CheckMemberNo(string szInput)
        {
            return Regex.IsMatch(szInput, MemberNoPattern);
        }

        public static bool CheckSeriNo(string szInput)
        {
            return Regex.IsMatch(szInput, SeriPattern);
        }

        public static bool CheckPassword(string szInput)
        { 
            return Regex.IsMatch(szInput,PasswordPattern);
        }

        public static int ToOneOrZero(string szInput,string EmptyResult)
        {
            return int.Parse(IsNullAndEmpty(szInput)?EmptyResult:szInput);
        }

        public static bool IsNullAndEmpty(string szInput)
        {
            if (szInput == null) return true;
            if (szInput == string.Empty) return true;
            return false;
        }

        public static bool CheckGuid(string szInput)
        { 
            return Regex.IsMatch(szInput,@"^[{|\(]?[0-9a-fA-F]{8}[-]?([0-9a-fA-F]{4}[-]?){3}[0-9a-fA-F]{12}[\)|}]?$");
        }

		//---------------------------------------------------------
		//Check valid numbers(0..9)
		public static bool CheckNormalNumbers(string szInput)
		{
			return Regex.IsMatch(szInput,"^[0-9]+$");
		}
		//---------------------------------------------------------
		//Get numbers only(0..9)
		public static string GetNumbersOnly(string szInput)
		{
			return Regex.Match(szInput,"^[0-9]+$").Value;
		}

        public static bool CheckPhoneNo(string szInput)
        {
            return CheckNormalNumbers(szInput);
        }
		//---------------------------------------------------------
		//Goal: Check if szInput is an Integer.
		public static bool CheckInt(string szInput)
		{
			int nCheck = -1;
			try
			{
				nCheck = int.Parse(szInput);
				return (nCheck >= 0);
			}
			catch
			{
				return false;
			}
		}
		//---------------------------------------------------------
		//Goal: Check if szInput is an Double.
		public static bool CheckDouble(string szInput)
		{
			double nCheck = -1;
			try
			{
				nCheck = double.Parse(szInput);
				return (nCheck >= 0);
			}
			catch
			{
				return false;
			}
		}
		//---------------------------------------------------------
		//Goal: Romove Blank from a string
		public static string RemoveBlankSpace(string szInput)
		{
			return szInput.Replace(" ", "");
		}
		//--------------------------------------------------------
		//Goal: Check the input email format
        
		public static bool CheckEmail(string szEmail)
		{
			Regex rex  = new  Regex(@"([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)", RegexOptions.IgnoreCase);
			return rex.IsMatch(szEmail);
		}

        public static bool CheckCurrency(string currency)
		{
            Regex rex = new Regex(@"^\$?([1-9]{1}[0-9]{0,2}(\,[0-9]{3})*(\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\.[0-9]{0,2})?|0(\.[0-9]{0,2})?|(\.[0-9]{1,2})?)$", RegexOptions.IgnoreCase);
            return rex.IsMatch(currency);
		}
        

		//--------------------------------------------------------
		//---------------------------------------------------------
		//Goal: Check if the input string is empty
		public static bool CheckBlank(string szInput)
		{
			return szInput.Trim().Equals("");
		}
		//--------------------------------------------------------
		//Goal: Check if the input string is a date format
		public static bool CheckDate(string szInput)
		{
			DateTime dtmCheck;
			try
			{
				dtmCheck = DateTime.Parse(szInput);
				return true;
			}
			catch
			{
				return false;
			}
		}
		//--------------------------------------------------------
		//Goal: Check valid numbers(0..9,a..z,A..Z)
		public static bool CheckNormalLetters(string szInput)
		{
			return Regex.IsMatch(szInput,"^[a-zA-Z0-9]+$");
		}
		//---------------------------------------------------------
		//Goal: Compare 2 strings
		public static bool CompareTWOStrings(string szInput1, string szInput2)
		{
			return szInput1.Equals(szInput2);
		}
		//---------------------------------------------------------
		
		//--------------------------------------------------------
		
		//--------------------------------------------------------
		//--------------------------------------------------------
		//Goal: Check if the text is an Double.
		
		//---------------------------------------------------------
		//Goal: Check if the text is an Double.
		
		//---------------------------------------------------------
		//Goal: Check if the text is an Double.
		//---------------------------------------------------------
		//Goal: Check if the text is an Double.
		public static string MoneyFormat(object obj)
		{
			string szMoney = "0.00";
			try
			{
				szMoney = double.Parse(obj.ToString()).ToString("#################0.00");
				return szMoney;
			}
			catch
			{
				return szMoney;
			}
		}
		//---------------------------------------------------------
		//Goal: Check if the text is an Double.
		//--------------------------------------------------------
		//Created 2006-08-18 by Nguyen Truc Minh
		//Check the if dataset has row
		public static bool CheckDataSetHasRow(ref DataSet ds)
		{
			if (ds==null) return false;
			if (ds.Tables.Count<=0) return false;
			if (ds.Tables[0].Rows.Count<=0) return false;
            //if (ds.Tables[1].Rows.Count <= 0) return false;
			return true;
		}
		//---------------------------------------------------------
		//Goal: Return value if we can do converting
		//		Otherwise return 0
		public static int ConvertToInt(object obj)
		{
			int nTem = 0;
			if (obj== null)
			{
				nTem = 0;
			}
			else
			{
				if (CheckInt(obj.ToString()))
				{
					nTem = int.Parse(obj.ToString());
				}
				else
				{
					nTem = 0;
				}
			}
			return nTem;
		}
		//---------------------------------------------------------
		//Created 2006-10-31 by Nguyen Truc Minh
		//Goal: Convert input object to boolean
		// Modified 2006-11-09: Convert integer to boolean
		public static bool ConvertToBool(object obj)
		{
			if (! CheckBool(obj)) return false;

			if(CheckInt(obj.ToString()))
			{
				return (ConvertToInt(obj) > 0); 
			}

			return bool.Parse(obj.ToString());
			
		}
		//---------------------------------------------------------
		//Created 2006-10-21 by Nguyen Truc Minh
		//Goal: Check if szInput is a Boolean.
		//Modified 2006-11-09: If input is interger value return true
		public static bool CheckBool(string szInput)
		{
			if (CheckInt(szInput)) return true;
			
			bool nCheck = false;
			try
			{
				nCheck = bool.Parse(szInput);
				return (true);
			}
			catch
			{
				return false;
			}
		}
		//---------------------------------------------------------
		//Created 2006-10-21 by Nguyen Truc Minh
		//Goal: Check if obj is a Boolean.
		// Overload 2: input is object
		public static bool CheckBool(object obj)
		{
			if(obj == null) return false;
			return CheckBool(obj.ToString());
			
		}
		//---------------------------------------------------------
		//Created 2006-10-27 (My Birthday) by Nguyen Truc Minh
		//Goal: Check if input string is an Vietnamese SSN.
		public static bool CheckSSN(string szInput)
		{
			string szPattern = @"^\d\d\d\d\d\d\d\d\d$";

			return System.Text.RegularExpressions.Regex.IsMatch(szInput, szPattern);
		}
		//--------------------------------------------------------
		//---------------------------------------------------------
		//Created 2007-06-06 by Nguyen Truc Minh
		//Goal: Check if input string is an Home phone. The pattern is defined in language file
		public static bool CheckHomePhone(string szInput)
		{
			string szPattern = @"^\d\d\d\d\d\d\d\d\d\d$";

			return System.Text.RegularExpressions.Regex.IsMatch(szInput, szPattern);
		}
		//--------------------------------------------------------
		
		//---------------------------------------------------------
		//Created 2007-06-06 by Nguyen Truc Minh
		//Goal: Check if input string is an Hand Phone. The pattern is defined in language file
		public static bool CheckHandPhone(string szInput)
		{
			string szPattern = @"^\d\d\d\d\d\d\d\d\d\d$";

			return System.Text.RegularExpressions.Regex.IsMatch(szInput, szPattern);
		}
		//--------------------------------------------------------
		
		//---------------------------------------------------------
		//Goal: Check if szInput is an Integer.
		public static bool CheckPositiveInt(string szInput)
		{
			int nCheck = -1;
			try
			{
				nCheck = int.Parse(szInput);
				return (nCheck > 0);
			}
			catch
			{
				return false;
			}
		}
		//--------------------------------------------------------

		//---------------------------------------------------------
		//Created 2006-11-16 by Nguyen Truc Minh
		//Goal: Check if szInput is an decimal.
		public static bool CheckDecimal(string szInput)
		{
			decimal nCheck = -1;
			try
			{
				nCheck = decimal.Parse(szInput);
				return (nCheck >= 0);
			}
			catch
			{
				return false;
			}
		}
		//--------------------------------------------------------
		
		//---------------------------------------------------------
		//Created 2006-11-16 by Nguyen Truc Minh
		//Goal: Check if szInput is an decimal.
		public static bool CheckPositiveDecimal(string szInput)
		{
			decimal nCheck = -1;
			try
			{
				nCheck = decimal.Parse(szInput);
				return (nCheck > 0);
			}
			catch
			{
				return false;
			}
		}
		//--------------------------------------------------------
		
		//---------------------------------------------------------
		//Created 2006-11-16 by Nguyen Truc Minh
		//Goal: Convert input object into double. Return 0 if we can do converting
		public static decimal ConvertToDecimal(object obj)
		{
			try
			{
				decimal nValue = decimal.Parse(obj.ToString());
				return nValue;
			}
			catch
			{
				return 0;
			}
		}
        
		//---------------------------------------------------------

        
	}
}
